/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : MqcpDeviceInfoReport.c
  * @brief          : MqcpDeviceInfoReport
  ******************************************************************************
  * @attention
  * 本文件用于  用户信息上报
  * @20210414:文件模板第一次生成
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */


#include "MqcpGetTime.h"
#include "SystemTimeListTask.h"
#include "SysUtcTime.h"
#include "SystemConfigTask.h"


static MqcpDataDef *MqcpGetTimeReqProcess(MqcpMessageDef *Message);
static MqcpDataDef *MqcpGetTimeRespProcess(MqcpMessageDef *Message);

static RTC_HandleTypeDef hrtc;

void GetTimeInit(void)//用户信息上报初始化
{
	MqcpRouterDef routeReq = 
	{
		1,
		TimeReq,
		MqcpGetTimeReqProcess
	};
	while(addRouter(routeReq));
	
	MqcpRouterDef routeResp = 
	{
		1,
		TimeResp,
		MqcpGetTimeRespProcess
	};
	while(addRouter(routeResp));
}

MqcpDataDef *MqcpGetTimeReqProcess(MqcpMessageDef *Message)
{
	MqcpDataDef *MqcpData = rt_malloc(sizeof(MqcpDataDef));
	
	if(NULL == MqcpData)
	{
		return NULL;
	}
	
	MqcpData->isNeedResp = 0;
	MqcpData->Size = 0;
	
	uint16_t MessageSize = 0;
	
	rt_memset(MqcpData->MqcpMessage.MsgId, 0, 12);MqcpData->Size = MqcpData->Size + 12;
	MqcpData->MqcpMessage.Sync = MqcpSyncFalse;MqcpData->Size = MqcpData->Size + 1;
	MqcpData->MqcpMessage.Code = MqcpCodeOK;MqcpData->Size = MqcpData->Size + 1;
	MqcpData->MqcpMessage.Ver = MQCP_USE_VER;MqcpData->Size = MqcpData->Size + 1;
	MqcpData->MqcpMessage.DevID[3] = MqcpDevice.DevId>>24;
	MqcpData->MqcpMessage.DevID[2] = MqcpDevice.DevId>>16;
	MqcpData->MqcpMessage.DevID[1] = MqcpDevice.DevId>>8;
	MqcpData->MqcpMessage.DevID[0] = MqcpDevice.DevId&0xff;MqcpData->Size = MqcpData->Size + 4;
	MqcpData->MqcpMessage.Cmd = TimeReq;MqcpData->Size = MqcpData->Size + 1;
	
	MqcpData->MqcpMessage.Len[1] = MessageSize>>8;
	MqcpData->MqcpMessage.Len[0] = MessageSize&0xff;MqcpData->Size = MqcpData->Size + 2;
	
	MqcpData->MqcpMessage.Data[MessageSize] = Chack_Xor((uint8_t*)(&(MqcpData->MqcpMessage)),MqcpData->Size);MqcpData->Size = MqcpData->Size + 1;
	
	return MqcpData;
}


DateTimeDef ServerDateTime;

uint32_t now_time = 0;

MqcpDataDef *MqcpGetTimeRespProcess(MqcpMessageDef *Message)
{
	SysDataDef *SysData = (SysDataDef *)(MqcpDataType.Modbus_SYS);
	
	
	MqcpDataDef *MqcpData = rt_malloc(sizeof(MqcpDataDef));
	
	if(NULL == MqcpData)
	{
		return NULL;
	}
	
	MqcpData->isNeedResp = Message->Sync;
	MqcpData->Size = 0;
	
	
	now_time = Message->Data[0];
	now_time |= Message->Data[1]<<8;
	now_time |= Message->Data[2]<<16;
	now_time |= Message->Data[3]<<24;
	
	SysSubSec = now_time + 3600 * SysData->Time_zone;
	
	UnixToDateTime(SysSubSec,&ServerDateTime);
	
	sTime.Hours 	= ServerDateTime.Hours;
	sTime.Minutes = ServerDateTime.Minutes;
	sTime.Seconds = ServerDateTime.Seconds;
	sTime.SubSeconds = 0;
	sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
	sTime.StoreOperation = RTC_STOREOPERATION_RESET;
	
	sDate.WeekDay = ServerDateTime.WeekDay;
	sDate.Month = ServerDateTime.Month;
	sDate.Date = ServerDateTime.Date;
	sDate.Year = ServerDateTime.Year - 2000;	
	
	tTimeSart = 2;
	
	return MqcpData;
}

/************************ (C) COPYRIGHT CunJu *****END OF FILE****/
